/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreRadioButton;
import spagcore.SpaglCoreRadioGroup;
import spagcore.SpaglCoreWidget;

/**
 * A class that organizes radio buttons for a swt application
 * @author Sam Pottinger
 * @see SpaglRadioButton
 */
public class SpaglRadioGroup implements SpaglCoreRadioGroup, SpaglCoreContainer {

	private static final long serialVersionUID = 1L;
	private ArrayList<SpaglRadioButton> buttons = new ArrayList<SpaglRadioButton>();
	private SpaglCoreContainer parentWidget;
	private SpaglCoreContainer myContainer;
	private boolean eventSpecified = false;
	private ArrayList<SpaglEventListener> events = new ArrayList<SpaglEventListener>(0);

	/**
	 * The minimal constructor for the SpaglRadioGroup
	 * 
	 * @param newParent The parent {@link SpaglContainer} where this widget will be placed.
	 */
	public SpaglRadioGroup(SpaglCoreContainer newParent)
	{
		parentWidget = newParent;
	}
	
	/**
	 * Combines placement and widget construction.
	 * 
	 * @param newParent The parent {@link SpaglContainer} where this widget will be placed
	 * @param placementParameters The string that will be passed to the parent container's layout manager (most likely MigLayout)
	 */
	public SpaglRadioGroup(SpaglCoreContainer newParent, String placementParameters)
	{
		parentWidget = newParent;
		parentWidget.placeWidget((SpaglContainer)myContainer, placementParameters);
	}
	
	/**
	 * Adds a new radio button to the group with the default layout behavior
	 * 
	 * @param newButton The new {@link SpaglRadioButton} button
	 */
	public SpaglCoreRadioButton addButton(SpaglCoreRadioButton newButton) {
		buttons.add((SpaglRadioButton)newButton);
		return newButton;
	}
	
	/**
	 * Adds a new radio button to the group with specified layout parameters
	 * 
	 * @param newButton The new {@link SpaglRadioButton} button
	 * @param placementParameters The placement parameters for the layout manager
	 */
	public SpaglCoreRadioButton addButton(SpaglCoreRadioButton newButton, String placementParameters) {
		buttons.add((SpaglRadioButton)newButton);
		myContainer.placeWidget(newButton, placementParameters);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}
	
	/**
	 * Creates a new radio button with the specified text and places it using the default layout behavior
	 * 
	 * @param newText The text for the new radio button
	 */
	public SpaglCoreRadioButton addButton(String newText) {
		SpaglCoreRadioButton newButton = new SpaglRadioButton(myContainer, newText);
		buttons.add((SpaglRadioButton)newButton);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}

	/**
	 * Creates a new radio button with the specified text and places it using the default layout behavior
	 * 
	 * @param newText The text for the new radio button
	 * @param placementParameters The information used to place the widget in the layout manager
	 */
	public SpaglCoreRadioButton addButton(String newText, String placementParameters) {
		SpaglCoreRadioButton newButton = new SpaglRadioButton(myContainer, newText);
		myContainer.placeWidget(newButton, placementParameters);
		buttons.add((SpaglRadioButton)newButton);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}
	
	/**
	 * Takes the {@link SpaglRadioButton}s and converts them to an array of {@link SpaglCoreRadioButton}s that are contained in this widget
	 * 
	 * @return the SpaglRadioButtons contained in the widget as an array of SpaglCoreRadioButtons
	 */
	public SpaglCoreRadioButton[] getButtons() {
		return (SpaglCoreRadioButton[])buttons.toArray();
	}

	/**
	 * Removes a button from the group at a given index
	 * 
	 * @param i the index of the element
	 */
	public void removeButton(int i) {
		buttons.get(i).dispose();
		buttons.remove(i);
	}

	/**
	 * Selects the button at the given index and unselects the rest
	 * 
	 * @param newSelection the index that is to be turned on
	 */
	public void setSelection(int newSelection) {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(i == newSelection)
				buttons.get(i).setSelected(true);
			else
				buttons.get(i).setSelected(false);
		}
	}
	
	/**
	 * Selects the button at the given index and unselects the rest
	 * 
	 * @param newSelection the SpaglCoreRadioButton that is to be turned on
	 */
	public void setSelection(SpaglCoreRadioButton newSelection) {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i) == newSelection)
				buttons.get(i).setSelected(true);
			else
				buttons.get(i).setSelected(false);
		}
	}

	/**
	 * Determines the selected radio button
	 * 
	 * @return the index of the button that is selected
	 */
	public int getSelectedIndex() {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i).isSelected())
				return i;
		}
		return -1;
	}
	
	/**
	 * Determines the selected radio button
	 * 
	 * @return the button that is selected
	 */
	public SpaglCoreRadioButton getSelectedButton() {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i).isSelected())
				return buttons.get(i);
		}
		return null;
	}

	/**
	 * Gets the parent container for the object
	 * 
	 * @return The {@link SpaglContainer} for this object as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the height in pixels of the object
	 * 
	 * @return Gets the height
	 */
	public int getHeight() {
		return myContainer.getHeight();
	}

	/**
	 * Gets the width in pixels of the object
	 * 
	 * @return Gets the height
	 */
	public int getWidth() {
		return myContainer.getWidth();
	}

	/**
	 * Sets the size of the object in pixels
	 * 
	 * @param width the horizontal size in size x
	 * @param height the vertical size in size y
	 */
	public void setSize(int width, int height) {
		myContainer.setSize(width, height);
	}

	/**
	 * Places a widget in this container
	 * 
	 * @param w {@link SpaglSwtWidget}
	 * @param parameters The string layout information to be passed on to the layout manager
	 */
	public void placeWidget(SpaglCoreWidget w, String parameters) {
		myContainer.placeWidget(w, parameters);
	}
	
	/**
	 * Adds an event listener to this check box
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param o The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object o) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {distributeEvent(new SpaglEventListener(o.getClass().getMethod(method, SpaglCoreEvent.class), o, SpaglEvent.SELECTION_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();}
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
	
	// Add the specified event listener to each of the buttons in this group and specify that an event has been choosen
	private void distributeEvent(SpaglEventListener listener) {
		for(SpaglRadioButton b : buttons)
			b.getNativeWidget().addListener(SWT.Selection, listener);
		eventSpecified = true;
		events.add(listener);
	}

	// Add the event listeners attached to this group to the specified button
	private void addEventsToButton(SpaglRadioButton newButton)
	{
		for(SpaglEventListener event : events)
			newButton.getNativeWidget().addListener(SWT.Selection, event);
	}
	
	// Swing does not need manual disposal, so this is here just for cross library purposes
	public void dispose() {}
}
